const Router = require('@koa/router');
const authController = require('../controllers/auth.controller');
const { checkAuth } = require('../middlewares/auth');

const router = new Router({
  prefix: '/api/v1/auth'
});

/**
 * @swagger
 * /auth/register:
 *   post:
 *     tags: [认证管理]
 *     summary: 用户注册
 *     description: 创建新用户账户
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - email
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *                 example: johndoe
 *               email:
 *                 type: string
 *                 format: email
 *                 description: 电子邮箱
 *                 example: john@example.com
 *               password:
 *                 type: string
 *                 format: password
 *                 description: 密码（至少8位，包含字母和数字）
 *                 example: Password123
 *               phoneNumber:
 *                 type: string
 *                 description: 手机号码
 *                 example: "13800138000"
 *     responses:
 *       201:
 *         description: 注册成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 注册成功
 *                 userId:
 *                   type: string
 *                   example: "12345"
 *       400:
 *         description: 输入数据无效
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 用户名已存在
 */
router.post('/register', authController.register);

/**
 * @swagger
 * /auth/login:
 *   post:
 *     tags: [认证管理]
 *     summary: 用户登录
 *     description: 验证用户身份并返回JWT令牌
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名或电子邮箱
 *                 example: johndoe
 *               password:
 *                 type: string
 *                 format: password
 *                 description: 用户密码
 *                 example: Password123
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 token:
 *                   type: string
 *                   description: JWT访问令牌
 *                   example: eyJhbGciOiJIUzI1NiIs...
 *                 refreshToken:
 *                   type: string
 *                   description: JWT刷新令牌
 *                   example: eyJhbGciOiJIUzI1NiIs...
 *                 user:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                       example: "12345"
 *                     username:
 *                       type: string
 *                       example: johndoe
 *       401:
 *         description: 认证失败
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 用户名或密码错误
 */
router.post('/login', authController.login);

/**
 * @swagger
 * /auth/logout:
 *   post:
 *     tags: [认证管理]
 *     summary: 用户登出
 *     description: 注销当前用户的登录状态
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 登出成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 登出成功
 *       401:
 *         description: 未认证
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 未登录或会话已过期
 */
router.post('/logout', checkAuth, authController.logout);

/**
 * @swagger
 * /auth/profile:
 *   get:
 *     tags: [认证管理]
 *     summary: 获取用户信息
 *     description: 获取当前登录用户的详细信息
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "12345"
 *                 username:
 *                   type: string
 *                   example: johndoe
 *                 email:
 *                   type: string
 *                   example: john@example.com
 *                 phoneNumber:
 *                   type: string
 *                   example: "13800138000"
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *   put:
 *     tags: [认证管理]
 *     summary: 更新用户信息
 *     description: 更新当前登录用户的个人信息
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *                 format: email
 *                 description: 新的电子邮箱
 *                 example: newmail@example.com
 *               phoneNumber:
 *                 type: string
 *                 description: 新的手机号码
 *                 example: "13900139000"
 *               password:
 *                 type: string
 *                 format: password
 *                 description: 新的密码（可选）
 *                 example: NewPassword123
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 个人信息更新成功
 *       400:
 *         description: 输入数据无效
 *       401:
 *         description: 未认证
 */
router.get('/profile', checkAuth, authController.getProfile);
router.put('/profile', checkAuth, authController.updateProfile);

module.exports = router; 